﻿using Microsoft.AspNetCore.Mvc;
using WMS.Common.WMS.Common;
using WMS.Model;
using WMS.Repository.IRepository;
using WMS.Service.IService;

namespace wmsWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;
        private readonly ILogger<MenuController> _logger;

        public MenuController(IMenuService menuService, ILogger<MenuController> logger)
        {
            _menuService = menuService;
            _logger = logger;
        }

        /// <summary>
        /// 获取菜单树列表
        /// </summary>
        [HttpGet("GetMenuListAsync")]
        public async Task<ActionResult<ResultApi>> GetMenuListAsync(bool? isEnabled = null, bool? isVisible = null)
        {
            try
            {
                var result = await _menuService.GetMenuListAsync(isEnabled, isVisible);
                return ResultHelper.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取菜单列表失败");
                return ResultHelper.Error("500", "获取菜单列表失败");
            }
        }

        /// <summary>
        /// 获取动态路由
        /// </summary>
        [HttpGet("GetDynamicRouterAsync")]
        public async Task<ActionResult<ResultApi>> GetDynamicRouterAsync(long userId)
        {
            try
            {
                var result = await _menuService.GetDynamicRouterAsync(userId);
                return ResultHelper.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取动态路由失败 - 用户ID: {userId}");
                return ResultHelper.Error("500", "获取动态路由失败");
            }
        }

        /// <summary>
        /// 获取一级菜单
        /// </summary>
        [HttpGet("GetOneMenuList")]
        public async Task<ResultApi> GetOneMenuList()
        {
            try
            {
                var (success, message, data) = await _menuService.GetOneMenuList();
                if (success)
                {
                    return ResultHelper.Success(data, message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取一级菜单失败");
                return ResultHelper.Error("500", "获取一级菜单失败");
            }
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        [HttpPost("AddMenuAsync")]
        public async Task<ResultApi> AddMenuAsync([FromBody] MenuModelDto menuModel)
        {
            try
            {
                if (menuModel == null)
                {
                    return ResultHelper.Error("菜单数据不能为空");
                }

                if (string.IsNullOrWhiteSpace(menuModel.MenuName) || string.IsNullOrWhiteSpace(menuModel.MenuCode))
                {
                    return ResultHelper.Error("菜单名称和编码不能为空");
                }

                if (menuModel.MenuType <= 0)
                {
                    return ResultHelper.Error("请选择菜单类型");
                }

                var (success, message) = await _menuService.AddMenuAsync(menuModel);
                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加菜单失败 - 菜单名称: {menuModel?.MenuName}");
                return ResultHelper.Error("500", "添加菜单失败");
            }
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        [HttpPost("UpdateMenuAsync")]
        public async Task<ResultApi> UpdateMenuAsync([FromBody] MenuModelDto menuModel)
        {
            try
            {
                if (menuModel == null)
                {
                    return ResultHelper.Error("菜单数据不能为空");
                }

                if (string.IsNullOrWhiteSpace(menuModel.MenuName) || string.IsNullOrWhiteSpace(menuModel.MenuCode))
                {
                    return ResultHelper.Error("菜单名称和编码不能为空");
                }

                if (menuModel.MenuType <= 0)
                {
                    return ResultHelper.Error("请选择菜单类型");
                }

                var (success, message) = await _menuService.UpdateMenuAsync(menuModel);
                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"修改菜单失败 - 菜单ID: {menuModel?.Id}, 菜单名称: {menuModel?.MenuName}");
                return ResultHelper.Error("500", "修改菜单失败");
            }
        }
    }
}